package com.dferreira.axil.data;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import com.dferreira.axil.model.Fornecedor;

@ApplicationScoped
public class RepositorioDeFornecedores {

    @Inject
    private EntityManager em;

    public Fornecedor findById(Long id) {
        return em.find(Fornecedor.class, id);
    }

    public List<Fornecedor> findAllOrderedByName() {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Fornecedor> criteria = cb.createQuery(Fornecedor.class);
        Root<Fornecedor> fornecedor = criteria.from(Fornecedor.class);
        criteria.select(fornecedor).orderBy(cb.asc(fornecedor.get("nome")));
        return em.createQuery(criteria).getResultList();
    }
}
